//
//  PictureBroswerCell.swift
//  测试-图片查看器
//
//  Created by shmily on 15/8/9.
//  Copyright © 2015年 shmilyAshen. All rights reserved.
//

import UIKit

class PictureBroswerCell: UICollectionViewCell {
    // 显示的图像
    var image: UIImage? {
        didSet{
            resetScrollView()
            
            imgView.image = image
            //设置图片的 frame 属性
            if image != nil {
                fitSize()
            }
        }
    }
    
    /// 重设 ScrollView 的偏移属性
    private func resetScrollView() {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }
    
    private func fitSize() {
        let retio = image!.size.height / image!.size.width
        let height = contentView.bounds.width * retio // 图像缩放后的高度
        let s = CGSize(width: contentView.bounds.width, height: height)
            
        if height > contentView.bounds.height { // 顶部显示
             imgView.frame = CGRect(origin: CGPointZero, size: s)
            scrollView.contentSize = s
        } else { // 居中显示
            let y = (contentView.bounds.height - height) * 0.5
            imgView.frame = CGRect(origin: CGPointZero, size: s)
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "acceptNotification:", name: kNotificationDidSelectItem, object: nil)
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func acceptNotification(ns: NSNotification) {
        
        print(ns.userInfo)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 布局视图
    private func setupUI() {
        contentView.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["sv":scrollView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["sv":scrollView]))
        
        scrollView.delegate = self
        scrollView.maximumZoomScale = 2.0
        scrollView.minimumZoomScale = 0.5
        print(scrollView.frame)
        scrollView.backgroundColor = UIColor.redColor() //TODO:- 颜色需要删掉
        scrollView.addSubview(imgView)
    }
    
    
    // scrollView collectionView 内部
    private lazy var scrollView: UIScrollView = UIScrollView()
    // imgView scrollView 的内部,显示图片
    private lazy var imgView: UIImageView = UIImageView()
}

extension PictureBroswerCell: UIScrollViewDelegate {
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imgView
    }
    
    //缩放完成后,移动位置
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
         //print(view?.frame)
        
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        // 如果边距小于0，需要修正
        offsetX = offsetX < 0 ? 0 : offsetX
        
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        offsetY = offsetY < 0 ? 0 : offsetY
        
        UIView.animateWithDuration(0.3) { () -> Void in
            scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
        }

    }
}

